/**
 * 出货信息维护
 * @author mengyb
 * @date 2019/05/08
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        vueData={},
        cols = [[
            {type: 'checkbox', title: '选择', fixed: 'left'}
            ,{field:'ROW_ID',width:60,title:'序号'}
            , {field: 'GWECDOCNO', width: 150, title: '出货单单号'}
            , {field: 'GWECSEQ', width: 80, title: '出货单项次'}
            , {field: 'GWEC003', width: 180, title: '销售订单号'}
            , {field: 'GWECDOCDATE', width: 100, title: '单据日期'}
            , {field: 'GWEC004', width: 100, title: '产品编号',search:'default',searchOpts:{icon:true}}
            , {field: 'GWAML003', width: 150, title: '品名'}
            , {field: 'GWAML004', width: 80, title: '规格'}
            , {field: 'GWCS002', width: 80, title: '销售单位'}
            , {field: 'GWCS003', width: 80, title: '订单数量'}
            , {field: 'COUNT', width: 80, title: '已出货数量'}
            , {field: 'GWAKL004', width: 80, title: '订单客户',search:'default',searchOpts:{icon:true}}
            , {field: 'GWCS016', width: 100, title: '预交日期'}
            , {field: 'GWEC002', width: 100, title: '出货日期',search:'default',searchOpts:{type:'date'}}
            , {field: 'GWEC009', width: 120, title: '本次出货数量'}
            , {field: 'NAME_USER', width: 80, title: '人员',search:'default',searchOpts:{icon:true}}
            , {field: 'NAME_ORG', width: 80, title: '归属组织'}
            , {field: 'GWEC013', width: 80, title: '归属设备'}
            , {field: 'GWEC015', width: 80, title: '备注'}
            , {field: 'GWEC016', width: 80, title: '有效否', filterDatas: app.datas_('sys_y_n')}
            , {field:'GWECCONF',width: 80, title: '状态码', filterDatas: app.datas_('crm_gwcd_gwcdacti')
                ,search:'default',searchOpts:{type:'select',default:''}}
            ,{
                width: 60,
                title: '',
                toolbar: '#script-gwei005setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwei005SettingAction,
        moduleName='出货',
        editArea=['1200px', '500px']
    ;

    var Gwei005 = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei005',
                _searchs: [{
                    id: 'gwei005', tableId: 'gwei005_table', datas: cols[0]
                }],
                _loadTplsFull: [
                    {dom: 'gwei005_edit', tpl: '/gwei005_edit'},
                    {dom: 'common_open_window', tpl: 'common/_open_window'},
                    {dom: 'gwei005_print', tpl: '/gwei005_print'}
                ]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwei005_table'),
                    url: g_ctxPath + 'crm/gwei005/getListData',
                    exportsUrl: g_ctxPath + 'base/gwei005/getListData',
                    exportsName: '出货信息资料',
                    importsUrl:g_ctxPath + 'base/gwei005/importsData',
                    exportsMbUrl:g_ctxPath + 'files/出货导入模板.xlsx',
                    title: moduleName+'表',
                    method: 'post',
                    id: 'gwei005_table',
                    size: _layuiConfig.ui_size,
                    autoSort: false, //禁用前端自动排序
                    defaultToolbar: ['filter', 'exports_import'],
                    toolbar: '#table-toolbar',
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-gwei005setting-action').innerHTML).render({}, function (string) {
                    gwei005SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwei005_table)', function (obj) {

                    layer.tips(gwei005SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        if(obj.data.GWECCONF != 'N'  ){
                                            app.showWarning("已审核的资料无法修改！");
                                            return ;
                                        };
                                        self._openTplFull({
                                            tag: 'gwei005_edit',
                                            tagData: {
                                                isAdd:false,
                                                isDetail:false,
                                                obj:obj,
                                                moduleName:moduleName
                                            },
                                            index: 0,
                                            openOpts: {
                                                title: moduleName+'编辑',
                                                minmax: true
                                            }
                                        });
                                        break;
                                    case '1':
                                        if(obj.data.GWECCONF != 'N'  ){
                                            app.showWarning("已审核的资料无法删除！");
                                            return ;
                                        }
                                        self.delRow(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwei005_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwei005_edit',
                        tagData: {
                            isAdd:false,
                            isDetail:true,
                            obj:obj,
                            moduleName:moduleName
                        },
                        index: 0,
                        openOpts: {
                            title: '查看'+moduleName,
                            minmax: true
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwei005_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwei005_edit',
                                tagData: {
                                    isAdd: true,
                                    isDetail:false,
                                    moduleName:moduleName
                                },
                                index: 0,
                                openOpts: {
                                    title: '新增'+moduleName,
                                    minmax: true
                                }

                            });
                            break;
                        case 'del':
                            self.delRows();
                            break;
                        case 'check':
                            self.check();
                            break;
                        case 'unCheck':
                            self.unCheck();
                            break;
                        case 'print':
                            self.print();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            //console.log(table_where);
            self._reloadTable({id: 'gwei005_table', where: table_where});
        },

        delRow: function (obj) {
            app.ajax({
                xurl: 'crm/gwei005/delRow',
                xdata: obj.data,
                loading: true,
                confirm: {msg: "确认删除该条数据吗？"},
                okmsg: moduleName +'信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });
        },
        delRows: function(){
            var checkStatus = table.checkStatus('gwei005_table');
            var flag = true;
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    if(v.GWECCONF == 'N'){
                        var _temp = app.fields_('crm_gwei005');
                        _temp.GWECSEQ = v.GWECSEQ;
                        _temp.GWECDOCNO = v.GWECDOCNO;
                        _xdata.push(_temp);
                    }else{
                        app.showWarning("已审核的资料无法删除！");
                        flag = false;
                        return ;
                    }

                });
                if(flag){
                    app.confirm({
                        msg:'确认删除所选择的信息吗？',
                        ok: function () {
                            app.ajax({
                                xurl:'crm/gwei005/delRows',
                                xdata:_xdata,
                                toJsonStr:true,
                                okmsg:moduleName+'信息删除成功',
                                loading:true,
                                cb: function (result) {
                                    if (result.ok) {
                                        self.reloadTableCurr();
                                    }
                                }
                            })
                        }
                    });
                }
            }else{
                app.showMessage('请先选择待删除的信息');
            }
        },

        //审核
        check: function () {
            var checkStatus = table.checkStatus('gwei005_table');
            if (checkStatus.data.length > 0) {
                var xdata=[];
                var flag=true;//是否满足审核条件
                checkStatus.data.forEach(function (value) {
                    if (value.GWECCONF == 'N') {
                        var xtemp={};
                        xtemp.GWECDOCNO=value.GWECDOCNO;
                        // xtemp.GWECSEQ=value.GWECSEQ;
                        xdata.push(xtemp);
                    }else {
                        flag=false;
                        return;
                    }
                });
                if(flag){//满足审核条件
                    app.ajax({
                        xurl:'crm/gwei005/check',
                        xdata:xdata,
                        loading:true,
                        toJsonStr:true,
                        confirm: {msg: "是否审核选择的数据？"},
                        okmsg:'出货信息审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }

                        }
                    })
                }else {//不满足审核条件
                    app.showWarning('不可重复审核');
                }

            }else{
                app.showWarning('请先选择待审核的记录');
            }
        },
        //取消审核
        unCheck: function () {
            var checkStatus = table.checkStatus('gwei005_table');
            if (checkStatus.data.length > 0) {
                var xdata = [];
                var flag=true;
                checkStatus.data.forEach(function (value) {
                    if (value.GWECCONF == 'Y') {
                        var xtemp = {};
                        xtemp.GWECDOCNO=value.GWECDOCNO;
                        // xtemp.GWECSEQ=value.GWECSEQ;
                        xdata.push(xtemp);
                    }else {
                        flag=false;
                        return;
                    }
                });
                if (flag) {//满足取消审核条件
                    app.ajax({
                        xurl: 'crm/gwei005/unCheck',
                        xdata: xdata,
                        loading: true,
                        toJsonStr: true,
                        confirm: {msg: "是否取消审核选择的数据？"},
                        okmsg: '出货信息取消审核成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                            }

                        }
                    });
                } else {//不满足取消审核条件
                    app.showWarning('不可重复取消审核');
                }
            }else{//未选择数据
                app.showWarning('请先选择待取消审核的记录');
            }
        },

        //打印
        print:function(){
            var checkStatus = table.checkStatus('gwei005_table');
            if(checkStatus.data.length>0){
                if(checkStatus.data.length>1){
                    app.showMessage('操作被取消，该业务只能操作单条记录，请重新选择。')
                    return;
                }
                app.pageModule('gwei005_print').print(checkStatus.data[0].GWECDOCNO);

            }else{
                app.showMessage('请先选择待操作的记录')
            }
        },

        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    data: vueData,
                    searchId: 'gwei005',
                    methods: {
                        handleIconClick:function (field) {
                            var vue = this;
                            if(field == 'GWAKL004'){//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gwak001_2',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWEC005', width: '150'},
                                                {label: '客户名称', prop: 'GWAKL004', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWAKL004 = data.GWEC005;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择客户',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if(field=='GWEC004'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_gwam001_1',
                                            columnList: [
                                                {label: '产品编号', prop: 'GWEC004', width: '150'},
                                                {label: '产品名称', prop: 'GWAML003', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWEC004 = data.GWEC004;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择产品',
                                        area: ['600px', '465px']
                                    }
                                });
                            }else if(field=='NAME_USER'){
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'cq_sysuser_id_1',
                                            columnList: [
                                                {label: '人员编号', prop: 'ID', width: '150'},
                                                {label: '人员名称', prop: 'NAME', width: '150'}
                                            ]
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.NAME_USER=data.ID;
                                        }
                                    },
                                    index: 1,
                                    openOpts: {
                                        title: '选择销售人员',
                                        area: ['600px', '465px']
                                    }
                                });
                            }
                        }
                    }
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwei005();

});
